package com.bobo.netty.server.controller;

import com.bobo.netty.base.ResponseResult;
import com.bobo.netty.service.UserService;
import com.bobo.netty.vo.UserFriendVO;
import com.bobo.netty.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * yangbo
 * 2020-01-04
 * netty-actual-combat
 **/
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 注册功能
     *
     * @param userVO
     * @return
     */
    @RequestMapping(value = "registerUser", method = RequestMethod.POST)
    public ResponseResult<String> registerUser(@RequestBody UserVO userVO) {
        if (StringUtils.isBlank(userVO.getUserName()) || StringUtils.isBlank(userVO.getPassWord())) {
            return ResponseResult.error("用户名、密码不能为空");
        }
        return userService.registerUser(userVO);
    }

    /**
     * 登陆功能
     *
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ResponseResult<String> login(@RequestParam("phone") String phone, @RequestParam("passWord") String passWord) {
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(passWord)) {
            return ResponseResult.error("用户名、密码不能为空");
        }
        UserVO userVO = new UserVO(null, phone, passWord);
        return userService.login(userVO);
    }

    /**
     * 查询列表|模糊查询
     *
     * @param userName
     * @return
     */
    @RequestMapping(value = "getAllUser", method = RequestMethod.GET)
    public ResponseResult<List<UserVO>> getAllUser(@RequestParam(value = "userName", required = false) String userName) {
        return userService.getAllUser(userName);
    }

    /**
     * 添加好友
     *
     * @param userFriendVO
     * @return
     */
    @RequestMapping(value = "addFriend", method = RequestMethod.POST)
    public ResponseResult<String> addFriend(@RequestBody UserFriendVO userFriendVO) {
        if (StringUtils.isBlank(userFriendVO.getUserId()) || StringUtils.isBlank(userFriendVO.getFriendId())) {
            return ResponseResult.error("好友列表不能为空");
        }
        return userService.addFriend(userFriendVO);
    }

    /**
     * 添加好友
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "getMyFriendList", method = RequestMethod.GET)
    public ResponseResult<List<UserVO>> getMyFriendList(@RequestParam(value = "id") String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.error("id不能为空");
        }
        return userService.getMyFriendList(id);
    }
}
